﻿using System;
using System.Collections.Generic;
using System.Text;

using Weazel.Badger.Vhdl;

namespace Weazel.Willows.InterfaceGeneration
{
	public class HardwareQueueRegistry
	{
		private class QueueContainer
		{
			public int Width;
			public int Length;
			public DesignFile DesignFile;
			public ComponentDeclaration ComponentDeclaration;

			public QueueContainer(int width, int length, DesignFile file)
			{
				this.Width = width;
				this.Length = length;
				this.DesignFile = file;
				this.ComponentDeclaration = file.Entities[0].GetDeclaration();
			}
		}

		private FifoElementRegistry elementRegistry = new FifoElementRegistry();

		private List<QueueContainer> queues = new List<QueueContainer>();

		public List<String> GetFileNames()
		{
			List<string> result = new List<string>();
			foreach (QueueContainer queue in queues)
				result.Add(queue.DesignFile.FileName);
			return result;
		}

		public void WriteToDisk(string outputFolder)
		{
			foreach (QueueContainer queue in queues)
				queue.DesignFile.Write(outputFolder);

			elementRegistry.WriteToDisk(outputFolder);
		}

		public HardwareQueueRegistry()
		{
		}

		private QueueContainer find(int width, int length)
		{
			foreach (QueueContainer queue in queues)
				if (queue.Length == length && queue.Width == width)
					return queue;

			return null;
		}

		public ComponentDeclaration GetQueueComponentDeclaration(int width, int length)
		{
			QueueContainer existing = find(width, length);

			if (existing != null)
			{
				return existing.ComponentDeclaration;
			}

			DesignFile queue = 
				HardwareQueueEntity.CreateQueueDesignFile(width, length, elementRegistry);			 

			QueueContainer container = new QueueContainer(width, length, queue);

			queues.Add(container);

			return container.ComponentDeclaration;
		}
	}
}
